<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
use Mail;

class HomeController extends Controller
{
    //首页
    public function shouye()
    {

     $navs = DB::table('nav')->get();
      
      return view('home/index',['nav'=>$navs,'request'=>$request,]);

    }
    
  

    //显示前台注册页面
    public function register()
    {
    	//显示模板
    	return view('home.register');
    }
    
    //注册功能
    public function doregister(Request $request)
    {
        dd($request->all());
        //进行表单验证
    	$this->validate($request,[
    			//用户名=>必填|格式|名字唯一
    			'username' => 'required|regex:/^\w{2,14}$/|unique:user,username',
    			//密码=>必填|格式|两次密码是否一直
    			'password' => 'required|regex:/^\S{4,16}$/|same:repassword',
    			//邮箱=>必填|格式
    			'email' => 'required|regex:/(\w*)@(\w*).com/'
    		],[
    			'username.required' => '用户名不能为空',
    			'username.regex' => '用户名格式不正确',
    			'username.unique' => '用户名已存在',
    			'password.required'=> '密码不能为空',
    			'password.regex' => '密码格式不正确',
    			'password.same' => '两次密码不一致',
    			'email.required' => '邮箱不能为空',
    			'email.regex' => '邮箱格式不正确'
    		]);
    	//获取数据
    	$data = $request->except(['repassword','agree','_token']);
        dd($data);

    	//加密
    	$data['password'] = Hash::make($data['password']);
        //增加一个字段 值随机
        $data['token'] = rand(000000,999999);

        //获取注册时间
        $data['register'] = date('Y-m-d',time());
        //获取默认头像
        $data['profile'] = '/images/tx.png';

        //给积分字段增加值
        $data['jifen'] += 2;


    	//插入数据
    	$id = DB::table('user')->insertGetId($data);

    	if($id){
            //发送邮件
    		Mail::send('home.jihuo', ['data'=>$data,'id'=>$id,'token'=>$data['token']], function ($m) use ($data){
                    $m->to($data['email'], $data['username'])->subject('论坛注册提醒!');
            });
        	// 检测
    	    return redirect('/home')->with('info','注册成功 ');
    	}else{
    		return back()->with('error','注册失败');
    	}
    }

    //显示找回密码表单
    public function forget()
    {
        return view('home.forget');
    }

    //获取邮箱地址并检测
    public function doforget(Request $request)
    {
        //读取数据库
        $res = DB::table('user')->where('email',$request->input('email'))->first();
        // dd($res);
        //检测是否存在
        if(empty($res)){
            return back()->with('error','邮箱不存在');
        }else{
            //如果存在  发送邮件
            Mail::send('home.email.forget', ['res'=>$res], function ($m) use ($res) {
                    //给谁发 名字是什么  发什么内容
                $m->to($res->email, $res->username)->subject('找回密码邮件!');
            });
            return redirect('/');
        }
    }

    //激活注册
    public function jihuo(Request $request)
    {
        //获取id
        $id = $request->input('id');

        $token = $request->input('token');

        $_token = DB::table('user')->where('id',$id)->first();

        if($token == $_token->token){
           //更新用户的状态
            $res = DB::table('user')->where('id',$id)->update(['status'=>1]);
            //提醒
            if($res){
                return redirect('/')->with('info','激活成功');
            }else{
                return redirect('/')->with('error','激活失败');
            } 
        }else{
            abort(404);
        }
        
    }
    
    //重置密码
    public function reset(Request $request)
    {
        //分配变量  将id与token分配过去
        return view('home.reset',['id'=>$request->input('id'),'token'=>$request->input('token')]);
    }

    //修改密码页面
    public function doreset(Request $request)
    {
       $this->validate($request,[
            'password' => 'required|regex:/^\S{4,20}$/|same:repassword'
        ],[
            'password.required' => '密码不能为空',
            'password.regex' => '密码格式不正确',
            'password.same' => '两次密码不一致'
        ]);
       //获取password
        $data['password'] = Hash::make($request->input('password'));

        $id = $request->input('id');

        $token = $request->input('token');
        
        $_token = DB::table('user')->where('id',$id)->first();

        if($token == $_token->token){
           //更新用户的状态
            $res = DB::table('user')->where('id',$id)->update($data);
            //提醒
            if($res){
                return redirect('/login')->with('info','激活成功');
            }else{
                return redirect('/reset')->with('error','激活失败');
            } 
        }else{
            abort(404);
        }
         
    }

    //显示登录页面
    public function login()
    {
       return view('home.login');

    }

    //执行登录
    public function dologin(Request $request)
    {
        // dd($request->all());
       //进行表单验证
    	$this->validate($request,[
			//用户名=>必填|格式|名字唯一
        'username' => 'required|regex:/^\w{8,18}$/|exists:user,username',
			'password' => 'required|regex:/^\S{6,20}$/'			
		],[
			'username.required' => '用户名不能为空',
            'username.exists' => '该用户不存在',
			'username.regex' => '用户名格式不正确',			
			'password.required'=> '密码不能为空',
            'password.regex' => '密码格式不正确'					
		]);

		$res = DB::table('user')->where('username',$request->input('username'))->first();
        
        //判断会员等级
        if($res->jifen>0 && $res->jifen<=60){
            return redirect('/home')->with('新手会员');
        }elseif($res->jifen>60 && $res->jifen<=300){
            return redirect('/home')->with('铜牌会员');
        }elseif($res->jifen>300 && $res->jifen<=600){
            return redirect('/home')->with('银牌会员');
        }elseif($res->jifen>600 && $res->jifen<=1200){
            return redirect('/home')->with('金牌会员');
        }else($res->jifen>1200){
            return redirect('/home')->with('钻石会员');
        }

    	if(!$res){
    		return back();
    	}else{
    		//检测密码
    		if(Hash::check($request->input('password'),$res->password)){

    			//写入session 跳转前台首页
    			session(['id'=>$res->id,'username'=>$res->username,'profile'=>$res->profile]);
    			return  redirect('/home')->with('info','登录成功');
    		}else{
    			return  back()->with('error','账号或密码错误');
    		}
    	}

    }


    //退出登录
    public function logout(Request $request)
    {
        $request->session()->forget('username');
        return redirect('/home')->with('info','正在跳转');
    }



    public function topic(Request $request)
    {
        // dd(123);
        return view('home.topic');
    }

    /**
     * 帖子历史表
     */
    public function card()
    {
        //后台查找分类信息
        $s = DB::table('session')->get();
        // dd($s);
        return  view('home.card.histry',['session1'=>$s]);
    }

    /**
     * 处理前台数据并插入数据库
     */
    public function docard(Request $request)
    {
        //获取数据
        $data = $request->except(['_token']);

        //插入数据
        $res = DB::table('card')->insertGetId($data);
        
        //检测
        if($res){
            return redirect('/homes/question')->with('info','添加成功 ');
        }else{
            return back()->with('error','添加失败');
        }
    }
